<?php
namespace Partner\Controller;

use Utuo\BackgroundController;
use Utuo\Region;
use Utuo\Phpass;

class SettingController extends BackgroundController{
	
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('合作商设置');
		$this->setActionTitle(array(
				'index' => '合作商基础信息',
		));
	}
	
	// 合作商信修改页
	public function indexAction(){
		$partner =  M("Partner")->find($this->aid);
		$partner['provinceOptions'] = $this->getOption(Region::getProvinceList(), $partner['province'], '==请选择省份==');
		$partner['cityOptions'] = $this->getOption(Region::getCityListByProvince($partner['province']), $partner['city'], '==请选择城市==');
		$partner['areaOptions'] = $this->getOption(Region::getAreaListByCity($partner['city']), $partner['area'], '==请选择地区==');
		$this->assign('partner', $partner);
		$this->display();
	}
	
	// 合作商信息修改
	public function indexPostAction(){
		$d = D('Partner');
		if ($d->create()) {
			$d->id = $this->aid;
			if (empty($d->password)) {
				unset($d->password);
			} else{
				$d->password = Phpass::hash($d->password);
			}
			if (false !== $d->save()) {
				$this->ajaxSuccess('修改合作商成功');
			} else {
				$this->ajaxError('修改合作商失败');
			}
		} else {
			$this->ajaxError('创建数据失败');
		}
	}
}